package com.ddxz.middleground.master.controller;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.user.ChangePassword;
import com.ddxz.middleground.master.dto.user.UserLogin;
import com.ddxz.middleground.master.dto.user.UserPage;
import com.ddxz.middleground.master.dto.user.UserInfo;
import com.ddxz.middleground.master.service.UserService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/27
 */

@RestController
@Api(tags = "用户管理")
public class UserController {
    public UserController(UserService service) {
        this.service = service;
    }

    @ApiOperation(value = "检索用户", consumes = "application/json", produces = "application/json",
            notes = "检索本部门下的所有用户，不含子部门下的用户")
    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public DdxzResponse getList(@DdxzRequestHeader MasterHeader header,
                                @DdxzRequestHeader UserPage pageParam) {
        return service.getUserList(header, pageParam);
    }

    @ApiOperation(value = "查看用户详情", consumes = "application/json", produces = "application/json",
            notes = "获取用户密码之外的所有信息")
    @RequestMapping(value = "/user/{userId}/detail", method = RequestMethod.GET)
    public DdxzResponse getDetail(@DdxzRequestHeader MasterHeader header,
                                  @PathVariable(name = "userId") String userId) {
        return service.getUserDetail(header, userId);
    }

    @ApiOperation(value = "查看基本信息", consumes = "application/json", produces = "application/json",
            notes = "查看用户自己的基本信息")
    @RequestMapping(value = "/myself/detail", method = RequestMethod.GET)
    public DdxzResponse getMyInfo(@DdxzRequestHeader MasterHeader header) {
        return service.getUserDetail(header, header.getUserId());
    }

    @ApiOperation(value = "保存用户", consumes = "application/json", produces = "application/json",
            notes = "若用户标识存在则修改已有用户，否则新增一个用户，登陆名称不允许重复，其他允许重复")
    @RequestMapping(value = "/user/save", method = RequestMethod.POST)
    public DdxzResponse saveUser(@DdxzRequestHeader MasterHeader header,
                                 @RequestBody UserInfo user) {
        return service.saveUser(user, header);
    }

    @ApiOperation(value = "修改密码", consumes = "application/json", produces = "application/json",
            notes = "修改用户的密码，更新密码过期时间")
    @RequestMapping(value = "/user/change/password", method = RequestMethod.PUT)
    public DdxzResponse changePassword(@DdxzRequestHeader MasterHeader header,
                                       @RequestBody ChangePassword changePassword) {
        return service.changePassword(changePassword, header);
    }

    @ApiOperation(value = "重置密码", consumes = "application/json", produces = "application/json",
            notes = "将用户的密码修改为初始密码，用户第一次登陆时必须修改")
    @RequestMapping(value = "/user/{userId}/reset", method = RequestMethod.PUT)
    public DdxzResponse resetPassword(@DdxzRequestHeader MasterHeader header,
                                      @PathVariable(name = "userId") String userId) {
        return service.resetPassword(userId, header);
    }

    @ApiOperation(value = "删除用户", consumes = "application/json", produces = "application/json",
            notes = "删除用户")
    @RequestMapping(value = "/user/{userId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse removeUser(@DdxzRequestHeader MasterHeader header,
                                   @PathVariable(name = "userId") String userId) {
        return service.removeUser(userId, header);
    }

    @ApiOperation(value = "用户登陆", consumes = "application/json", produces = "application/json",
            notes = "使用RSA加密的用户名、密码完成登陆操作")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public DdxzResponse login(@RequestBody UserLogin login) {
        return service.login(login);
    }

    @ApiOperation(value = "绑定角色", consumes = "application/json", produces = "application/json",
            notes = "将一个用户绑定到一个或多个角色上")
    @RequestMapping(value = "/user/{userId}/grant", method = RequestMethod.PUT)
    public DdxzResponse grantUser(@DdxzRequestHeader MasterHeader header,
                                  @PathVariable(name = "userId") String userId,
                                  @RequestBody List<String> roleIds) {
        return service.grantUser(header, userId, roleIds);
    }

    private final UserService service;
}
